Page({
  data: {
    pageIndex: 1,
    productList: [],
    loading: true,
    noMore: false
  },
  onLoad: function (options) {
    this.getProduct();
  },
  onPullDownRefresh: function () {
    this.setData({
      pageIndex: 1
    }, () => {
      this.getProduct();
      wx.stopPullDownRefresh();
    });
  },
  onReachBottom: function () {
    if (this.data.noMore) return;
    this.setData({
      pageIndex: this.data.pageIndex + 1
    }, () => {
      this.getProduct();
    });
  },
  getProduct: function () {
    let page = this.data.pageIndex;
    let curUnix = wx.time.curUnix();
    wx.util.request('', {
      body: {page: page}
    }).then(res => {
      let list = (page == 1 ? [] : this.data.productList);
      for (let item of res) {
        // 默认保留两位小数，因此先取整再除100得到折扣价
        item.discount = Number(item.discount);
        let price = Math.round(item.product.price * item.discount) / 100;
        // 根据设置格式化折扣价
        // 0：保留角和分；1：抹去角和分；2：抹去分
        if (item.decimal_reservation_number == 1) {
          price = Math.floor(price);
        } else if (item.decimal_reservation_number == 2) {
          price = Number(price.toFixed(1));
        }
        item.price = price;
        item.startTime = wx.time.toDate(item.start_time);
        item.endTime = wx.time.toDate(item.end_time);
        item.start_time = Number(item.start_time);
        item.end_time = Number(item.end_time);
        list.push(item);
      }
      this.setData({
        productList: list,
        curUnix: curUnix,
        loading: false,
        noMore: res.length < 10
      });
    }).catch(err => {
      console.log(err);
    })
  },
  detail: function (e) {
    wx.navigateTo({
      url: `../product/detail?id=${e.currentTarget.dataset.id}`,
    })
  }
})